package com.firesrc.service.client;

import java.sql.Connection;

import jodd.db.DbQuery;
import jodd.util.StringUtil;

import com.firesrc.db.DBManager;
import com.firesrc.util.SqlTsvTool;
import com.firesrc.util.SystemConfigConstant;

public class AutoTask extends SuperClientTask {

  private static final String WHERE = "where (flag !=" + SystemConfigConstant.KEYWORD_STATE_UPLOAD + " or flag is null) and state='1'";
  private static final String SUB_KEYWORD_SQL = "select top 5 distinct name from keyword " + WHERE;

  @Override
  protected boolean isEmptyRst(String handleData) {
    if (StringUtil.isEmpty(handleData)) {
      return true;
    }
    String emptyString = "ID,MAIL,URL,TOPIC,KEYWORD,CONTENT,MAILSTATE,SENDMAILDATE\r\n";
    if (emptyString.equals(handleData)) {
      //      String sql = "update keyword set flag=" + SystemConfigConstant.KEYWORD_STATE_UPLOAD + WHERE;
      //      Connection connection = DBManager.getPublicConnectionPool().getConnection();
      //      new DbQuery(connection, sql).executeUpdateAndClose();
      System.out.println("AutoTask.run()无数据,不进行执行" + "返回");//fixme 要关闭Connection 发布时候要取消
      return true;
    }
    return false;
  }

  @Override
  public void handleRespString(String text) {
    String sql = "update keyword set flag=" + SystemConfigConstant.KEYWORD_STATE_UPLOAD + " where name in" + text;
    Connection connection = DBManager.getPublicConnectionPool().getConnection();
    new DbQuery(connection, sql).executeUpdateAndClose();
    DBManager.getPublicConnectionPool().closeConnection(connection);
  }

  @Override
  protected String getOpenatorType() {
    return SystemConfigConstant.IMPORT_RECORD;
  }

  @Override
  public String getSendDataString() {
    String sql = "SELECT  * FROM record where keyword in (" + SUB_KEYWORD_SQL + ") ";
    return SqlTsvTool.getStringNoFile(sql);
  }
}